package com.cxz.demo;

import annotation.SoiScan;
import com.cxz.demo.model.UserInfo;
import context.SoiContext;
import core.DefaultSOIContainer;
import enums.SOINodeTypeEnum;
import load.impl.DefaultSoiLoader;
import model.ChainKey;
import model.ExecutionRecord;
import model.SoiNodeBean;

import java.util.Arrays;
import java.util.List;

/**
 * @className: DemoTwo
 * @author: ocxz1
 * @date: 2025/3/26 下午5:36
 * @Version: 1.0
 * @description: soi
 */
@SoiScan(scanPackages = {"com.cxz.demo.service","com.cxz.demo.loader"})
public class DemoTwo {
    public static void main(String[] args) {

        DefaultSoiLoader defaultSoiLoader = new DefaultSoiLoader(DemoTwo.class);
        defaultSoiLoader.init();

        DefaultSOIContainer defaultSOIContainer = new DefaultSOIContainer();

        // 创建一个链路
        ChainKey chainKey = new ChainKey("test", "test");

        // 开始注册
        SoiNodeBean soiNodeBean = new SoiNodeBean();
        soiNodeBean.setId(1L);
        soiNodeBean.setNodeName("baseCheckUser-node");
        soiNodeBean.setNodeCode("test-AA");
        soiNodeBean.setChainKey(chainKey.getKey());
        soiNodeBean.setNodeType(SOINodeTypeEnum.START_NODE.name());
        soiNodeBean.setSoiUnitIdentifier("baseCheckUser");
        soiNodeBean.setCheckRuleScript("return userInfo.getName() == \"张三\"");
        soiNodeBean.setParamsJsonStr("[{\"name\":\"userInfo\",\"type\":\"com.cxz.demo.model.UserInfo\"}]");

        SoiNodeBean soiNodeBean2 = new SoiNodeBean();
        soiNodeBean2.setId(2L);
        soiNodeBean2.setNodeName("测试节点BB");
        soiNodeBean2.setNodeCode("test-BB");
        soiNodeBean2.setNodeName("loadUserInfo-node");
        soiNodeBean2.setChainKey(chainKey.getKey());
        soiNodeBean2.setSoiUnitIdentifier("loadUserInfo");
        soiNodeBean2.setParentId(1L);

        // 注册节点
        defaultSOIContainer.register(Arrays.asList(soiNodeBean, soiNodeBean2));

        SoiContext soiContext = new SoiContext();
        soiContext.setSourceCode("test-001");
        UserInfo userInfo = new UserInfo();
        userInfo.setId(1L);
        userInfo.setName("张三");
        soiContext.putBusinessData(userInfo);

        defaultSOIContainer.beginOrchestration(chainKey, soiContext);
        List<ExecutionRecord> executionRecordList = soiContext.getExecutionRecordList();
        System.out.println(executionRecordList);
    }
}
